package com.ebelter.seneo.ui.activitys;


import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserCurrentMeasureRecord;
import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserinfos;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.beans.HostoryListData;
import com.ebelter.seneo.contants.IUserContant;
import com.ebelter.seneo.models.SeneoDbUtils;
import com.ebelter.seneo.models.SeneoNet;
import com.ebelter.seneo.ui.fragments.main.Dash_Fragment;
import com.ebelter.seneo.ui.views.SeneoTipDialog2;
import com.ebelter.seneo.utils.SeneoUtils;

import org.greenrobot.eventbus.EventBus;

public class SeneoHistoryItemDetailActivity extends SeneoBaseActivity {


    public static final String ITEMDATD = "1";

    public static final String[] SIZES = new String[]{
            StringUtils.getResStr(R.string.yxfpx),
            StringUtils.getResStr(R.string.fpx),
            StringUtils.getResStr(R.string.ppx),
            StringUtils.getResStr(R.string.ydbzx),
            StringUtils.getResStr(R.string.biaozhun),
            StringUtils.getResStr(R.string.jrx),
            StringUtils.getResStr(R.string.pshx),
            StringUtils.getResStr(R.string.pshjrx),
            StringUtils.getResStr(R.string.jrfdx)
    };


    ImageView top_iv;
    TextView top_tv1;
    ImageView top_iv2;

    TextView hostory_d_time_tv;

    TextView hostory_weight_tv;
    TextView hostory_qzhtzh_tv;
    TextView hostory_tzhkzh_tv;
    TextView hostory_bmi_tv;
    TextView hostory_bodyfatpercentage_tv;
    TextView hostory_bmr_tv;
    TextView hostory_vfl_tv;
    TextView hostory_musclemass_tv;
    TextView hostory_bonemincontent_tv;
    TextView hostory_protein_tv;
    TextView hostory_bodywaterpercentage_tv;
    TextView hostory_skeletal_tv;
    TextView hostory_bodysore_tv;
    TextView hostory_bodyage_tv;
    TextView hostory_size_tv;

    HostoryListData.ResultDataBean.DataBean itemData;

    @Override
    protected int loadLayoutById() {
        return R.layout.hostoryitemdetail_ly;
    }


    @Override
    protected void initView() {
        FV();

        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        top_tv1.setText(R.string.history);
        ViewUtils.displayView(top_tv1);

        top_iv2.setImageResource(R.drawable.ic_delete);

        top_iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDeleteTip();
            }
        });

    }

    private SeneoTipDialog2 tipDialog2;
    private void showDeleteTip() {
        tipDialog2=new SeneoTipDialog2(this,StringUtils.getResStr(R.string.shfshchctshj));//是否删除此条记录
        tipDialog2.setListener(new SeneoTipDialog2.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }

            @Override
            public void ok(Dialog dialogView, String str) {
                dialogView.cancel();


                deleteByid(itemData.getId(),itemData.getUserId());
            }
        });
        tipDialog2.show();
    }

    private void deleteByid(final String id,final long userid) {

        SeneoNet.getIntance().deleteMeasureDataById(this, id, new HttpResponse<BaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse, String s) {
                if (isRequestSuccess&&baseResponse!=null&&baseResponse.resultCode==0){
                    //说明删除成功
                    LogUtils.i2("ItemDetailActivity","删除数据成功 id="+id);


                    //让列表页面更新一下listview数据
                    CommonEventBus commonEventBus=new CommonEventBus("ItemDetailActivity",SeneoHistoryListActivity.TAG, IUserContant.ICMDS.DELETE_DATA_SUCCESS,"删除数据成功");

                    Object[] objects=new Object[2];
                    objects[0]=id;
                    objects[1]=userid;
                    commonEventBus.setContent(objects);

                    EventBus.getDefault().post(commonEventBus);



                    ToastUtil.show(R.string.shchchg);//删除成功
                    finish();
                }else {
                    //说明删除失败
                    LogUtils.i2("ItemDetailActivity","删除数据失败 id="+id);
                    ToastUtil.show(R.string.shchshb);//删除失败
                }
            }
        });
    }

    @Override
    protected void initData() {
        itemData = (HostoryListData.ResultDataBean.DataBean) getIntent().getSerializableExtra(ITEMDATD);
        if (itemData==null){
            finish();
            return;
        }
        int unit = SeneoUtils.getUnitModel();//0kg 1lb
        String unitS=unit==0? IUserContant.IDIS.kg: IUserContant.IDIS.lb;

        ViewUtils.setTextViewStr(hostory_d_time_tv,SeneoUtils.timeFatmat(itemData.getTestDate(),3));//时间

        ViewUtils.setTextViewStr(hostory_weight_tv, getDisStr((float) itemData.getWeight(),unit,unitS));//
        ViewUtils.setTextViewStr(hostory_qzhtzh_tv, getDisStr( itemData.getToFatWeight(),unit,unitS));//
        ViewUtils.setTextViewStr(hostory_tzhkzh_tv, getDisStr4(itemData.getWeightControl(),unit,unitS));//

        ViewUtils.setTextViewStr(hostory_bmi_tv, getDisStr3((int) itemData.getBmi(),""));
        ViewUtils.setTextViewStr(hostory_bodyfatpercentage_tv, getDisStr2((float) itemData.getAdiposeRate(),"%"));
        ViewUtils.setTextViewStr(hostory_bmr_tv, getDisStr3(itemData.getBmr(),"kcal"));
        ViewUtils.setTextViewStr(hostory_vfl_tv, getDisStr2((float) itemData.getVisceralFat(),""));
        ViewUtils.setTextViewStr(hostory_musclemass_tv, getDisStr((float) itemData.getMuscleQuantity(),unit,unitS));
        ViewUtils.setTextViewStr(hostory_bonemincontent_tv, getDisStr((float) itemData.getBoneMass(),unit,unitS));
        ViewUtils.setTextViewStr(hostory_protein_tv, getDisStr2((float) itemData.getProtein(),"%"));
        ViewUtils.setTextViewStr(hostory_bodywaterpercentage_tv, getDisStr2((float) itemData.getMoisture(),"%"));
        ViewUtils.setTextViewStr(hostory_skeletal_tv, getDisStr((float) itemData.getBoneMineralContent(),unit,unitS));
        ViewUtils.setTextViewStr(hostory_bodysore_tv, getDisStr3(itemData.getBodySocre(),""));


        int age= NumUtils.numBaoLiuWei0((float) itemData.getPhysicalAge());
        ViewUtils.setTextViewStr(hostory_bodyage_tv,age==0?Dash_Fragment.Non:(age+"years old"));


        if (itemData.getSize()>=0){
            int index=itemData.getSize();
            if (index<0){
                index=0;
            }else if (index>=9){
                index=8;
            }
            ViewUtils.setTextViewStr(hostory_size_tv, SIZES[index]);
        }



    }


    private String getDisStr(float value,int unit,String unitS){
        if (value==0){
            return Dash_Fragment.Non;
        }else {
            return NumUtils.numBaoLiuWei1(unit==0? value :SeneoUtils.kg2lb(value))+unitS;
        }
    }

    private String getDisStr2(float value,String unitS){
        if (value==0){
            return Dash_Fragment.Non;
        }else {
            return NumUtils.numBaoLiuWei1(value)+unitS;
        }
    }

    private String getDisStr3(int value,String unitS){
        if (value==0){
            return Dash_Fragment.Non;
        }else {
            return NumUtils.numBaoLiuWei0(value)+unitS;
        }
    }

    private String getDisStr4(float value,int unit,String unitS){
        if (value==0){
            return Dash_Fragment.Non;
        }else {

            float r=NumUtils.numBaoLiuWei1(unit==0? value :SeneoUtils.kg2lb(value));
            String zhf="";
            if (r>0){
                zhf="+";
            }

            return zhf+r+unitS;
        }
    }

    private void FV() {
        top_iv=findViewById(R.id.top_iv);
        top_tv1=findViewById(R.id.top_tv1);
        top_iv2=findViewById(R.id.top_iv2);

        hostory_d_time_tv=findViewById(R.id.hostory_d_time_tv);

        hostory_weight_tv=findViewById(R.id.hostory_weight_tv);
        hostory_qzhtzh_tv=findViewById(R.id.hostory_qzhtzh_tv);
        hostory_tzhkzh_tv=findViewById(R.id.hostory_tzhkzh_tv);
        hostory_bmi_tv=findViewById(R.id.hostory_bmi_tv);
        hostory_bodyfatpercentage_tv=findViewById(R.id.hostory_bodyfatpercentage_tv);
        hostory_bmr_tv=findViewById(R.id.hostory_bmr_tv);
        hostory_vfl_tv=findViewById(R.id.hostory_vfl_tv);
        hostory_musclemass_tv=findViewById(R.id.hostory_musclemass_tv);
        hostory_bonemincontent_tv=findViewById(R.id.hostory_bonemincontent_tv);
        hostory_protein_tv=findViewById(R.id.hostory_protein_tv);
        hostory_bodywaterpercentage_tv=findViewById(R.id.hostory_bodywaterpercentage_tv);
        hostory_skeletal_tv=findViewById(R.id.hostory_skeletal_tv);
        hostory_bodysore_tv=findViewById(R.id.hostory_bodysore_tv);
        hostory_bodyage_tv=findViewById(R.id.hostory_bodyage_tv);
        hostory_size_tv=findViewById(R.id.hostory_size_tv);
    }
}
